package com.jtlrm.ckd.mvp.view.adapter;

import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.base.sdk.util.ImageUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jtlrm.ckd.R;
import com.jtlrm.ckd.util.ContactList.SlideBarEntity;

import java.util.HashSet;
import java.util.Set;

public class ContactsSeachAdapter extends BaseQuickAdapter<SlideBarEntity, BaseViewHolder> {

    private Set<SlideBarEntity> existMembers;

    private Set<String> cantEditMembers; // 不能编辑的群成员


    public ContactsSeachAdapter() {
        super(R.layout.item_seach_contacts);
        if (existMembers == null)
            existMembers = new HashSet<>();
    }

    @Override
    protected void convert(BaseViewHolder helper, final SlideBarEntity item) {
        helper.setText(R.id.name, item.getNameStr());
        ImageUtil.loadCircleAvatarImage((ImageView) helper.getView(R.id.avatar), item.getAvatar());
        LinearLayout container = helper.getView(R.id.container);
        final CheckBox select = helper.getView(R.id.checkbox);
        if (canEdit(item)) {
            // 能够编辑
            select.setEnabled(true);
            container.setEnabled(true);
            select.setButtonDrawable(R.drawable.checkbox_bg_selector);
            if (existMembers.contains(item)) {
                select.setChecked(true);
            } else {
                select.setChecked(false);
            }
        } else {
            select.setEnabled(false);
            container.setEnabled(false);
            select.setButtonDrawable(R.drawable.select_grey);
        }
        container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (select.isChecked()) {
                    select.setChecked(false);
                    if (existMembers.contains(item)) {
                        existMembers.remove(item);
                    }
                } else {
                    select.setChecked(true);
                    if (!existMembers.contains(item)) {
                        existMembers.add(item);
                    }
                }
            }
        });
        select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 因为顺序的原因
                if (select.isChecked()) {
                    select.setChecked(true);
                    if (!existMembers.contains(item)) {
                        existMembers.add(item);
                    }
                } else {
                    select.setChecked(false);
                    if (existMembers.contains(item)) {
                        existMembers.remove(item);
                    }
                }
            }
        });
    }

    /**
     * 判断是否能编辑
     *
     * @return
     */
    public boolean canEdit(SlideBarEntity item) {
        if (cantEditMembers != null && cantEditMembers.contains(item.getuId())) {
            return false;
        }
        return true;
    }

    public void setCantEditMembers(Set<String> cantEditMembers) {
        this.cantEditMembers = cantEditMembers;
        notifyDataSetChanged();
    }


    public Set<SlideBarEntity> getExistMembers() {
        return existMembers;
    }

    public void setExistMembers(Set<SlideBarEntity> existMembers) {
        this.existMembers = existMembers;
        notifyDataSetChanged();
    }
}
